package com.zhongxi.guoyuan.adapter;


import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Paint;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AlertDialog;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.entity.user.center.CollectionData;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;
import com.zhongxi.guoyuan.utils.DataUtil;
import com.zhongxi.guoyuan.utils.GlideRoundTransform;

import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2016/12/13.
 */

public class CollectionAdapter extends BaseAdapter {

    private Context context;
    private List<CollectionData> data;
    private JSONObject jsonExt;
    private Handler handler;
    private JSONObject optString;

    public CollectionAdapter(Context context, List<CollectionData> data, JSONObject jsonExt, Handler handler) {
        this.context = context;
        this.data = data;
        this.jsonExt = jsonExt;
        this.handler = handler;
    }

    @Override
    public int getCount() {
        if (data != null && data.size() > 0) {
            return data.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            switch (data.get(0).getFtype()) {
                case 1:
                    holder = new ViewHolder();
                    convertView = LayoutInflater.from(context).inflate(R.layout.adapter_meirong, parent, false);
                    holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
                    holder.collect_beauty_name = (TextView) convertView.findViewById(R.id.collect_beauty_name);
                    holder.collect_beauty_time = (TextView) convertView.findViewById(R.id.collect_beauty_time);
                    holder.collect_beauty_originPrice = (TextView) convertView.findViewById(R.id.collect_beauty_originPrice);
                    holder.workmanship = (TextView) convertView.findViewById(R.id.workmanship);
                    holder.tvCash = (TextView) convertView.findViewById(R.id.tvCash);
                    holder.collect_beauty_delete = (TextView) convertView.findViewById(R.id.collect_beauty_delete);
                    break;
                case 2:
                    holder = new ViewHolder();
                    convertView = LayoutInflater.from(context).inflate(R.layout.adapter_reward_coin, parent, false);
                    holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
                    holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
                    holder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
                    holder.tvSend = (TextView) convertView.findViewById(R.id.tvSend);
                    holder.tvDelete = (TextView) convertView.findViewById(R.id.tvDelete);
                    break;

            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        String refid = data.get(position).getRefid();
        switch (data.get(position).getFtype()) {
            case 1:
                holder.collect_beauty_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        AlertDialog dialog = new AlertDialog.Builder(context).setTitle("您确定要删除该项目吗？")
                                .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Message msg = Message.obtain();
                                        msg.obj = position;
                                        handler.sendMessage(msg);
                                        dialog.dismiss();
                                    }
                                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                }).show();
                        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(context.getResources().getColor(R.color.bg_yield));
                        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(context.getResources().getColor(R.color.bg_yield));

                    }
                });
                break;
            case 2:
                holder.tvDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        AlertDialog dialog = new AlertDialog.Builder(context).setTitle("您确定要删除该项目吗？")
                                .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Message msg = Message.obtain();
                                        msg.obj = position;
                                        handler.sendMessage(msg);
                                        dialog.dismiss();
                                    }
                                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                }).show();
                        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(context.getResources().getColor(R.color.bg_yield));
                        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(context.getResources().getColor(R.color.bg_yield));
                    }
                });
                break;
        }


        if (refid != null) {
            optString = jsonExt.optJSONObject(refid);
            if (null != optString && !"".equals(optString)) {
                switch (data.get(position).getFtype()) {
                    case 1://美容院

                        if (null != optString && !"".equals(optString)) {
                            String thumbnail = optString.optString("thumbnail");
                            Glide.with(context).load(Url.IMG_URL + thumbnail).placeholder(R.mipmap.iv_eg).into(holder.ivImg);

                            if (null != optString.optString("name") && !"".equals(optString.optString("name"))) {
                                holder.collect_beauty_name.setText(optString.optString("name"));
                            } else {
                                holder.collect_beauty_name.setText("");
                            }

                            if (null != optString.optString("marketprice") && !"".equals(optString.optString("marketprice"))) {
                                holder.collect_beauty_originPrice.setText("市场价" + optString.optString("marketprice") + "元");
                                holder.collect_beauty_originPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                            } else {
                                holder.collect_beauty_originPrice.setText("");
                            }

                            if (null != optString.optString("innerprice") && !"".equals(optString.optString("innerprice"))) {
                                holder.tvCash.setText(optString.optString("innerprice") + "美豆");
                            } else {
                                holder.tvCash.setText("");
                            }
                            if (null != optString.optString("servicetime") && !"".equals(optString.optString("servicetime"))) {
                                holder.collect_beauty_time.setText(optString.optString("servicetime") + "分钟");
                            } else {
                                holder.collect_beauty_time.setText("");
                            }

                            if (null != optString.optString("redbeanprice") && !"".equals(optString.optString("redbeanprice"))) {
                                holder.workmanship.setText("+" + optString.optString("redbeanprice") + "红豆");
                            } else {
                                holder.workmanship.setText("");
                            }

//                                holder.collect_beauty_delete.setOnClickListener(new View.OnClickListener() {
//                                    @Override
//                                    public void onClick(View view) {
//                                        Message msg = Message.obtain();
//                                        msg.obj = position;
//                                        handler.sendMessage(msg);
//                                    }
//                                });

                        }

                        break;
                    case 2://超市
                        if (null != optString && !"".equals(optString)) {

                            String thumbnail = optString.optString("thumbnail");
                            Glide.with(context).load(Url.IMG_URL + thumbnail).placeholder(R.mipmap.iv_eg).into(holder.ivImg);

                            if (null != optString.optString("name") && !"".equals(optString.optString("name"))) {
                                holder.tvName.setText(optString.optString("name"));
                            } else {
                                holder.tvName.setText("");
                            }
                            //goodstype   3:奖红豆  1:赠美豆
                            String goodstype = optString.optString("goodstype");
                            if (null != goodstype && !"".equals(goodstype)) {
                                if (goodstype.equals("3")) {

                                    holder.tvSend.setTextColor(context.getResources().getColor(R.color.title_red));
//                                        if (Float.parseFloat(optString.optString("floorprice")) < Float.parseFloat(optString.optString("peakprice"))) {
//
//                                            holder.tvSend.setText(context.getResources().getString(R.string.award_hongdou) + optString.optString("floorprice") + "红豆~" + optString.optString("peakprice") + "红豆");
//                                            holder.tvPrice.setText(optString.optString("floorprice") + "元~" + optString.optString("peakprice") + "元");
//                                        } else {
//                                            holder.tvSend.setText(context.getResources().getString(R.string.award_hongdou) + optString.optString("floorprice") + "红豆");
//                                            holder.tvPrice.setText(optString.optString("floorprice") + "元");
//                                        }
                                    if (null != optString.optString("floorprice") && !"".equals(optString.optString("floorprice"))) {
                                        holder.tvSend.setText(context.getResources().getString(R.string.award_hongdou) + optString.optString("floorprice") + "红豆");
                                        holder.tvPrice.setText(optString.optString("floorprice") + "元");
                                    } else {
                                        holder.tvSend.setText("");
                                        holder.tvPrice.setText("");
                                    }
                                } else {

//                                        if (Float.parseFloat(optString.optString("floorprice")) < Float.parseFloat(optString.optString("peakprice"))) {
//
//                                            holder.tvSend.setText(context.getResources().getString(R.string.give_meidou) + optString.optString("floorprice") + "美豆~" + optString.optString("peakprice") + "美豆");
//                                            holder.tvPrice.setText(optString.optString("floorprice") + "元~" + optString.optString("peakprice") + "元");
//                                        } else {
//                                            holder.tvSend.setText(context.getResources().getString(R.string.give_meidou) + optString.optString("floorprice") + "美豆");
//                                            holder.tvPrice.setText(optString.optString("floorprice") + "元");
//
//                                        }
                                    if (null != optString.optString("floorprice") && !"".equals(optString.optString("floorprice"))) {
                                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                            holder.tvSend.setText(Html.fromHtml("<font color='#ff7f00'>" + context.getResources().getString(R.string.give_meidou) + CommonTool.getWhole(optString.optString("floorprice"))
                                                    + "美豆+</font><font color='#ff4c4c'>" + DataUtil.multiplys(optString.optString("floorprice"), "0.05", 2) + "红豆</font>", Html.FROM_HTML_MODE_COMPACT));
                                        } else {
                                            holder.tvSend.setText(Html.fromHtml("<font color='#ff7f00'>" + context.getResources().getString(R.string.give_meidou) + CommonTool.getWhole(optString.optString("floorprice"))
                                                    + "美豆+</font><font color='#ff4c4c'>" + DataUtil.multiplys(optString.optString("floorprice"), "0.05", 2) + "红豆</font>"));
                                        }
                                        holder.tvPrice.setText(optString.optString("floorprice") + "元");
                                    } else {
                                        holder.tvSend.setText("");
                                        holder.tvPrice.setText("");
                                    }

                                }
                            }

//                                holder.tvDelete.setOnClickListener(new View.OnClickListener() {
//                                    @Override
//                                    public void onClick(View view) {
//                                        Message msg = Message.obtain();
//                                        msg.obj = position;
//                                        handler.sendMessage(msg);
//                                    }
//                                });

                        }

                        break;

                }
            }

        }
        return convertView;
    }

    class ViewHolder {
        private ImageView ivImg;
        private TextView collect_beauty_name;
        private TextView collect_beauty_time;
        private TextView collect_beauty_originPrice;
        private TextView workmanship;
        private TextView tvCash;
        private TextView collect_beauty_delete;

        private TextView tvName;
        private TextView tvPrice;
        private TextView tvSend;
        private TextView tvDelete;

    }

}
